﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Pivotbrowser.Models;
using Pivotbrowser.Web;
using Pivotbrowser.Web.Services;

namespace Pivotbrowser.Views
{
    public partial class Details : UserControl
    {
        public Popup Popup { get; set; }
        private MinimalStudent MinimalStudent { get; set; }
        private NotesContext Context { get; set; }
        private Notizen NotizEntity { get; set; }

        //public FrameworkElement Head { get { return this.gHead; } }

        public Details(Popup popup)
        {
            Popup = popup;
            MinimalStudent = popup.DataContext as MinimalStudent;
            this.Context = new NotesContext();
            var query = Context.GetNotizensQuery(MinimalStudent.MATRNR);
            InitializeComponent();
            Context.Load(query, (loadOp) =>
            {
                try
                {
                    this.NotizEntity = loadOp.Entities.First();
                    this.tbNotes.Text = NotizEntity.NOTIZ;
                    this.cbMarker.SelectedItem = this.cbMarker.Items.Where(x => (x as ComboBoxItem).Content.ToString() == loadOp.Entities.First().MARKER).First();
                }
                catch
                {
                    if (loadOp.Entities.Count() < 1)
                        this.NotizEntity = null;
                }
            }
            , null);

            if (MinimalStudent.AltePrüfungen.Count() > 0)
                this.hbSwitcher.Visibility = Visibility.Visible;
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (NotizEntity == null)
            {
                var n = new Notizen() { MATRNR = MinimalStudent.MATRNR, NOTIZ = this.tbNotes.Text.ToString(), MARKER = (this.cbMarker.SelectedItem as ComboBoxItem).Content.ToString() };

                if (!Context.Notizens.Contains(n))
                    Context.Notizens.Add(n);

                this.NotizEntity = n;
            }
            else
            {
                NotizEntity.NOTIZ = this.tbNotes.Text.ToString();
                NotizEntity.MARKER = (this.cbMarker.SelectedItem as ComboBoxItem).Content.ToString();
            }

            Context.SubmitChanges((callback) => { if (callback.HasError) { this.tbSaveMsg.Text = "Fehler.";} else {this.tbSaveMsg.Text = "Änderung gespeichert.";}}, null);
        }

        private bool bachelor = false;
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            if (!bachelor)
            {
                this.datagrid.ItemsSource = MinimalStudent.AltePrüfungen;
                this.hbSwitcher.Content = "Zeige aktuelle Prüfungen";
                bachelor = true;
            }
            else
            {
                this.datagrid.ItemsSource = MinimalStudent.p_collection_all;
                this.hbSwitcher.Content = "Zeige alte Prüfungen";
                bachelor = false;
            }
        }


        private void tbClose_Click(object sender, RoutedEventArgs e)
        {
            this.Popup.IsOpen = false;
        }

        private bool rawViewActive = false;
        private RawView rawView = null;
        private void tbRaw_Click(object sender, RoutedEventArgs e)
        {
            if (!rawViewActive)
            {
                rawView = new RawView(this.MinimalStudent);
                this.LayoutRoot.Children.Add(rawView);
                rawViewActive = true;
            }
            else
            {
                this.LayoutRoot.Children.Remove(rawView);
                rawViewActive = false;
            }

        }
    }
}
